#include "menu.h"
#include "ui_menu.h"
#include <QDebug>
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QMouseEvent>

map1::map1()
{
    int gx[48]={3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10};
    int gy[48]={3,4,5,6,7,8,3,4,5,6,7,8,1,2,9,10,1,2,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,5,6,7,8,9,10,1,2,5,6,7,8,9,10};
    for (int i=0;i<48;i++)
    {
        map11[gx[i]][gy[i]+3]=1;
    }
    int ax[48]={1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8};
    int ay[48]={3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10,1,2,7,8,1,2,7,8,1,2,7,8,1,2,7,8,3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10};
    for (int i=0;i<48;i++)
    {
        map11[ax[i]+13][ay[i]+3]=1;
    }
    int mx[52]={1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10};
    int my[52]={3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10,1,2,1,2,3,4,5,6,7,8,3,4,5,6,7,8,1,2,1,2,3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10};
    for (int i=0;i<52;i++)
    {
        map11[mx[i]+24][my[i]+3]=1;
    }
    int ex[48]={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7};
    int ey[48]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,9,10};
    for (int i=0;i<48;i++)
    {
        map11[ex[i]+37][ey[i]+3]=1;
        map11[ex[i]+37][ey[i]+31]=1;
    }
    int ox[40]={1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8};
    int oy[40]={3,4,5,6,7,8,3,4,5,6,7,8,1,2,9,10,1,2,9,10,1,2,9,10,1,2,9,10,3,4,5,6,7,8,3,4,5,6,7,8};
    for (int i=0;i<40;i++)
    {
        map11[ox[i]+13][oy[i]+17]=1;
    }
    int fx[40]={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,8,8};
    int fy[40]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,5,6,1,2,5,6,1,2,5,6,1,2,5,6,1,2,1,2};
    for (int i=0;i<40;i++)
    {
        map11[fx[i]+25][fy[i]+17]=1;
        map11[fx[i]+25][fy[i]+31]=1;
    }
    int lx[32]={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
    int ly[32]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,9,10,9,10,9,10,9,10,9,10,9,10};
    for (int i=0;i<32;i++)
    {
        map11[lx[i]][ly[i]+31]=1;
    }
    int ix[36]={1,1,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,6,6,6,6};
    int iy[36]={1,2,9,10,1,2,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,9,10,1,2,9,10};
    for (int i=0;i<36;i++)
    {
        map11[ix[i]+12][iy[i]+31]=1;
    }
}

map1 map1;
QTimer *timer1=new QTimer();
int speed1=0;

Menu::Menu(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Menu)
{
    ui->setupUi(this);
    connect(timer1,&QTimer::timeout,this,&Menu::timerUpdate);
    timer1->stop();
}

Menu::~Menu()
{
    delete ui;
}

void Menu::on_gamepushButton_clicked()
{
    this->close();
    game *g =new game();
    g->show();
}

void Menu::on_storypushButton_clicked()
{
    this->close();
    encounter *e =new encounter();
    e->show();
}

void Menu::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //设置网格颜色
    QPen pen(Qt::gray);
    painter.setPen(pen);

    //以label左上角为原点
    painter.translate(ui->label->x(),ui->label->y());

    //清空画布
       QBrush brush(Qt::white,Qt::SolidPattern);
       painter.setBrush(brush);
       painter.drawRect(0,0,ui->label->width()-1,ui->label->height()-1);

    //绘制网格
    for(int x=0;x<=map1.getwidthnum();x++)
    {
        painter.drawLine(x*map1.getwidth(),0,x*map1.getwidth(),map1.getheightnum()*map1.getheight());
    }
    for(int y=0;y<=map1.getheightnum();y++)
    {
        painter.drawLine(0,y*map1.getheight(),map1.getwidthnum()*map1.getwidth(),y*map1.getheight());
    }

    //绘制细胞
    brush.setColor(Qt::black);
    painter.setBrush(brush);
    for(int x=0;x<map1.getwidthnum();x++)
    {
        for(int y=0;y<map1.getheightnum();y++)
        {
            if(map1.getmap(x,y)==1)
                    painter.drawRect(x*map1.getwidth(),y*map1.getheight(),map1.getwidth(),map1.getheight());
        }
    }
    painter.end();
}

void Menu::timerUpdate()
{
    map1.mapupdate();
    update();//更新界面
}

void Menu::on_continuepushButton_clicked()
{
    timer1->start(500);
}

void Menu::on_renewpushButton_clicked()
{
    map1.mapclear();
    int gx[48]={3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10};
    int gy[48]={3,4,5,6,7,8,3,4,5,6,7,8,1,2,9,10,1,2,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,5,6,7,8,9,10,1,2,5,6,7,8,9,10};
    for (int i=0;i<48;i++)
    {
        map1.map11[gx[i]][gy[i]+3]=1;
    }
    int ax[48]={1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8};
    int ay[48]={3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10,1,2,7,8,1,2,7,8,1,2,7,8,1,2,7,8,3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10};
    for (int i=0;i<48;i++)
    {
        map1.map11[ax[i]+13][ay[i]+3]=1;
    }
    int mx[52]={1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10};
    int my[52]={3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10,1,2,1,2,3,4,5,6,7,8,3,4,5,6,7,8,1,2,1,2,3,4,5,6,7,8,9,10,3,4,5,6,7,8,9,10};
    for (int i=0;i<52;i++)
    {
        map1.map11[mx[i]+24][my[i]+3]=1;
    }
    int ex[48]={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7};
    int ey[48]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,5,6,9,10,1,2,9,10};
    for (int i=0;i<48;i++)
    {
        map1.map11[ex[i]+37][ey[i]+3]=1;
        map1.map11[ex[i]+37][ey[i]+31]=1;
    }
    int ox[40]={1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8};
    int oy[40]={3,4,5,6,7,8,3,4,5,6,7,8,1,2,9,10,1,2,9,10,1,2,9,10,1,2,9,10,3,4,5,6,7,8,3,4,5,6,7,8};
    for (int i=0;i<40;i++)
    {
        map1.map11[ox[i]+13][oy[i]+17]=1;
    }
    int fx[40]={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,8,8};
    int fy[40]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,5,6,1,2,5,6,1,2,5,6,1,2,5,6,1,2,1,2};
    for (int i=0;i<40;i++)
    {
        map1.map11[fx[i]+25][fy[i]+17]=1;
        map1.map11[fx[i]+25][fy[i]+31]=1;
    }
    int lx[32]={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
    int ly[32]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,9,10,9,10,9,10,9,10,9,10,9,10};
    for (int i=0;i<32;i++)
    {
        map1.map11[lx[i]][ly[i]+31]=1;
    }
    int ix[36]={1,1,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,6,6,6,6};
    int iy[36]={1,2,9,10,1,2,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,9,10,1,2,9,10};
    for (int i=0;i<36;i++)
    {
        map1.map11[ix[i]+12][iy[i]+31]=1;
    }
    update();
    timer1->stop();
}

void map1::mapupdate()
{
    int dx[8]={0,0,-1,1,-1,1,-1,1};
    int dy[8]={1,-1,0,0,1,1,-1,-1};

    memset(neighbor,0,sizeof(neighbor));//细胞清零

    //统计邻居细胞数量
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            if(map11[x][y]==1)
            {
                for(int i=0;i<8;i++)
                {
                    if(dx[i]+x>=0 and dx[i]+x<=widthnum and dy[i]+y>=0 and dy[i]+y<=heightnum) //防止越界
                    {
                        neighbor[dx[i]+x][dy[i]+y]++;//
                    }
                }
            }
        }
    }

    //改变细胞状态
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            if(neighbor[x][y]==3)
                map11[x][y]=1;
            else if(neighbor[x][y]!=2)
                map11[x][y]=0;
        }
    }
}

void map1::mapclear()
{
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            map11[x][y]=0;
        }
    }
}
